package com.lm.sharding.controller;

import com.lm.sharding.model.Order;
import com.lm.sharding.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/orders")
public class OrderController {

    private final OrderService service;

    public OrderController(OrderService service) {
        this.service = service;
    }

    @PostMapping("/create")
    public String create(@RequestParam Long userId) {
        Order order = new Order();
        order.setOrderId(ThreadLocalRandom.current().nextLong(10000, 99999));
        order.setUserId(userId);
        order.setAmount(ThreadLocalRandom.current().nextDouble(50, 500));
        order.setStatus("CREATED");
        service.createOrder(order);
        return "Order created for user " + userId;
    }

    @GetMapping("/user/{userId}")
    public List<Order> findByUser(@PathVariable Long userId) {
        return service.getOrdersByUser(userId);
    }
}